/*
 * Copyright (c) 2020 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package me.yokeyword.fragmentation.extensions;

import ohos.aafwk.ability.fraction.Fraction;
import ohos.aafwk.ability.fraction.FractionManager;

import java.util.List;
import java.util.Optional;

/*
 * Wrapper support setArguments and BackStack record
 * */

public class FractionManagerEx {
    FractionManager fm;
    FractionBackStack fbs = new FractionBackStack();
    FractionSchedulerEx fsex;

    public FractionManagerEx(FractionManager fm) {
        this.fm = fm;
        fsex = new FractionSchedulerEx(fm.startFractionScheduler(), fbs);
    }

    public Fraction getFractionByTag​(String tag) {
        Optional<Fraction> f = fm.getFractionByTag(tag);
        return f.isPresent() ? f.get() : null;
    }

    public FractionEx getFractionExByTag​(String tag) {
        return fbs.getFractionExByTag(tag);
    }

    public void popFromStack​() {
        fm.popFromStack();
        fbs.pop();
    }

    public void popFromStack​(String tag, int flags) {
        fm.popFromStack(tag, flags);
        fbs.pop();
    }

    public FractionSchedulerEx startFractionScheduler() {
        fsex = new FractionSchedulerEx(fm.startFractionScheduler(), fbs);
        return fsex;
    }

    public List<FractionEx> getActiveFractions() {
        return fbs.getActiveFractions();
    }

    public FractionEx findFractionByTag(String targetTag) {
        return fbs.findFractionByTag(targetTag);
    }

    public long getBackStackEntryCount() {
        return fbs.size();
    }

    public FractionEx getBackStackEntryAt(int i) {
        return fbs.get(i);
    }

    public FractionEx findActiveFractionEx(Fraction fraction) {
        return fbs.findActiveFractionEx(fraction);
    }

    public FractionEx checkNewRecordOrAdd(Fraction fraction) {
        return fbs.checkNewRecordOrAdd(fraction);
    }

    public FractionEx checkNewRecordOrAdd(Fraction fraction, String tag) {
        return fbs.checkNewRecordOrAdd(fraction).setTag(tag);
    }

    public void removeDummyEntries() {
        fbs.removeDummyEntries();
    }
}
